package org.springcloud.wisdom.springcloud_03_freshman.student.service;

import org.springcloud.wisdom.springcloud_03_freshman.index.login.entity.Freshman;
import org.springcloud.wisdom.springcloud_03_freshman.index.login.entity.Student;
import org.springcloud.wisdom.springcloud_03_freshman.student.dao.FreshmanDao;
import org.springcloud.wisdom.springcloud_03_freshman.student.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FreshmanService {
    @Autowired
    FreshmanDao freshmanDao;
    @Autowired
    StudentDao studentDao;

    /**
     * 根据学号更新新生表中的信息
     * @param freshman
     * @return
     */
    @Transactional
    public boolean update(Freshman freshman){
        if (freshman ==null){
            return  false;
        }
        if (freshman.getSid() == null ||
                freshman.getSid().trim().length() < 9 || freshman.getSid().trim().length()>9){
            return false;
        }
        Student student=new Student();
        student.setAddress(freshman.getAddress());
        student.setEmail(freshman.getEmail());
        student.setPhone(freshman.getPhone());
        student.setSid(freshman.getSid());
        studentDao.updateStudentBySid(student);
        return freshmanDao.UpdateFreshmanBySid(freshman)>0;
    }

    /**
     * 根据adsno获取新生的信息
     * @param adsno
     * @return
     */
    public Freshman getFresh(String adsno){
        if (adsno ==null|| adsno.trim().length()< 13 || adsno.trim().length()> 13 ){
            return null;
        }
        return freshmanDao.getFreshmanBySid(adsno);
    }
}
